import 'package:flutter/material.dart';
import '../model/CartGoods.dart';
import '../components/stepper.dart';


class CartPage extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {
    return _CartState();
  }
}

class _CartState extends State<CartPage> {
  List cartGoodsList = new List();
  ScrollController scrollController = new ScrollController(initialScrollOffset: 0.0);
  bool isCheckAll = false;
  int payMethod = 0;

  @override
  void initState() {
    cartGoodsList.addAll([
      new CartGoods(img: 'assets/images/p1.jpg',title: '哇哈哈营养快线哇哈哈营养快线哇哈哈营养快线', price: 8.25, total: '1箱10瓶装', spec: '4.2元/瓶' ,num: '1' ,isChecked: false),
      new CartGoods(img: 'assets/images/p1.jpg',title: '哇哈哈营养快线', price: 8.25, total: '1箱10瓶装', spec: '4.2元/瓶' ,num: '1' ,isChecked: false),
      new CartGoods(img: 'assets/images/p1.jpg',title: '哇哈哈营养快线', price: 8.25, total: '1箱10瓶装', spec: '4.2元/瓶' ,num: '1' ,isChecked: false),
      new CartGoods(img: 'assets/images/p1.jpg',title: '哇哈哈营养快线', price: 8.25, total: '1箱10瓶装', spec: '4.2元/瓶' ,num: '1' ,isChecked: false),
      new CartGoods(img: 'assets/images/p1.jpg',title: '哇哈哈营养快线', price: 8.25, total: '1箱10瓶装', spec: '4.2元/瓶' ,num: '1' ,isChecked: false),
      new CartGoods(img: 'assets/images/p1.jpg',title: '哇哈哈营养快线', price: 8.25, total: '1箱10瓶装', spec: '4.2元/瓶' ,num: '1' ,isChecked: false),
      new CartGoods(img: 'assets/images/p1.jpg',title: '哇哈哈营养快线', price: 8.25, total: '1箱10瓶装', spec: '4.2元/瓶' ,num: '1' ,isChecked: false),
    ]);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: PreferredSize(
        child: AppBar(
          elevation: 0.0,
        ),
        preferredSize: Size.fromHeight(MediaQuery.of(context).size.height * 0.06),
      ),
      body: Container(
//        height: 320.0,
        child: Column(
          children: <Widget>[
            Expanded(flex: 1, child: _RenderCustomList()),
            Container(
              child: Row(
                children: <Widget>[
                  Container(
                    width: 110.0,
                    height: 45.0,
                    child: Row(
                      children: <Widget>[
                        Checkbox(
                          value: isCheckAll,
                          onChanged: (bol) {
                            setState(() {
                              isCheckAll = bol;
                              cartGoodsList.forEach((cartGoods) {
                                cartGoods.isChecked = bol;
                              });
                            });
                          }
                        ),
                        Text('全选')
                      ],
                    )
                  ),
                  Container(
                    width: 100.0,
                    height: 45.0,
                    child: FlatButton(
                        color: Colors.orangeAccent,
                        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(0.0)),
                        textColor: Colors.white,
                        child: Text('删除'),
                        onPressed: () =>{}
                    ),
                  ),
                  Expanded(
                    child: Container(
//                      width: 97.0,
                      height: 45.0,
                      child: FlatButton(
                          color: Colors.red,
                          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(0.0)),
                          textColor: Colors.white,
                          child: Text('立即购买'),
                          onPressed: () {
//                          Routes.router.navigateTo(context, '${Routes.cardGoodsList}',transition: TransitionType.fadeIn);
                          }
                      ),
                    )
                  )
                ],
              ),
            ),
          ],
        )
      ),
    );
  }

  Widget _RenderCustomList() {
    return ListView(
      padding: EdgeInsets.only(bottom: 0.0),
      controller: scrollController,
      children: <Widget>[
        _RenderList(),
        Divider(color: Colors.grey,),
        Container(
          height: 45.0,
          margin: EdgeInsets.only(left: 15.0),
          child: Row(
            children: <Widget>[
              Text('合计:',style: TextStyle(color: Colors.grey),),
              Container(
                margin: EdgeInsets.only(left: 8.0),
                child: Text.rich(TextSpan(
                  children: [
                    TextSpan(
                        text: '￥',
                        style: TextStyle(fontSize: 12,color: Colors.red)
                    ),
                    TextSpan(
                        text: '2583.00',
                        style:  TextStyle(fontSize: 18,fontWeight: FontWeight.w800,color: Colors.red)
                    ),
                  ],
                )),
              ),
            ],
          ),
        ),
        Container(
          height: 15.0,
          color: Colors.grey[200],
        ),
        Container(
          height: 25.0,
          margin: EdgeInsets.only(left: 15.0,top: 15.0),
          child: Text('付款方式',style: TextStyle(fontSize: 16,color: Colors.grey),),
        ),
        Divider(color: Colors.grey,),
        Container(
          height: 60.0,
          margin: EdgeInsets.only(left: 15.0,bottom: 15.0),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              FlatButton(
                color: payMethod == 0 ? Colors.orange: Colors.grey[200] ,
                child: Text('微信',style: TextStyle(fontWeight: FontWeight.w400),),
                textColor: payMethod == 0 ? Colors.white: Colors.black87 ,
                onPressed: () => setState(() { payMethod = 0; })
              ),
              FlatButton(
                  color: payMethod == 1 ? Colors.orange: Colors.grey[200] ,
                  child: Text('支付宝',style: TextStyle(fontWeight: FontWeight.w400)),
                  textColor: payMethod == 1 ? Colors.white: Colors.black87 ,
                  onPressed: () => setState(() { payMethod = 1; })
              ),
              FlatButton(
                  color: payMethod == 2 ? Colors.orange: Colors.grey[200] ,
                  child: Text('现金',style: TextStyle(fontWeight: FontWeight.w400)),
                  textColor: payMethod == 2 ? Colors.white: Colors.black87 ,
                  onPressed: () => setState(() { payMethod = 2; })
              )
            ],
          ),
        ),
        Container(
          height: 60.0,
          color: Colors.grey[200],
        )
      ],
    );
  }

  Widget _RenderList() {
    return ListView.separated(
        shrinkWrap: true,
        physics:NeverScrollableScrollPhysics(),
        itemBuilder: (BuildContext context, int index) {
          var cartGoods = cartGoodsList[index];
          return ListTile(
            contentPadding:EdgeInsets.only(top: 4.0),
            leading: Container(
                width: 110.0,
                height: 100.0,
                child: Row(
                  children: <Widget>[
                    Checkbox(
                        tristate:false,
                        value: cartGoods.isChecked,
                        onChanged: (bool) {
                          setState(() {
                            isCheckAll = false;
                            cartGoods.isChecked = bool;
                          });
                        }
                    ),
                    Image.asset(cartGoods.img)
                  ],
                )
              ),
//            isThreeLine: true,
            title: Container(
//              margin: EdgeInsets.only(left: 0.0),
              alignment: Alignment.topLeft,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text('${cartGoods.title}',style: TextStyle(fontSize: 14),),
                  Text('${cartGoods.total}',style: TextStyle(fontSize: 12,color: Colors.grey),),
                  Row(
                    children: <Widget>[
                      Text.rich(TextSpan(
                        children: [
                          TextSpan(
                              text: '￥',
                              style: TextStyle(fontSize: 12,color: Colors.red)
                          ),
                          TextSpan(
                              text: '${cartGoods.price}',
                              style:  TextStyle(fontSize: 15,fontWeight: FontWeight.w500,color: Colors.red)
                          ),
                        ],
                      )),
                      Container(
                        margin: EdgeInsets.only(left: 8.0),
                        child: Text('${cartGoods.spec}',style: TextStyle(fontSize: 12,color: Colors.grey),),
                      )
                    ],
                  ),
                ],
              ),
            ),
            trailing: Container(
              margin: EdgeInsets.only(top: 25.0),
              width: 100.0,
              child: SyStepper(
                onChange: (v) => {},
              ),
            )
          );
        },
        separatorBuilder: (BuildContext context, int index) => Divider(color: Colors.grey,),
        itemCount: cartGoodsList.length
    );
  }
}